package com.ruoyi.system.util.xfyun.sign;
import okhttp3.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class DeepSeekClient {
    private static final String API_URL = "https://api.deepseek.com/chat/completions";
    private static final String API_KEY = "sk-fedafa7a616a4751917c64ac4ac986f8"; // 替换为你的API密钥
    private static final String MODEL = "deepseek-chat"; // 根据实际模型名称修改

    // 创建 OkHttpClient 并设置超时时间
    private final OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(180000, TimeUnit.SECONDS) // 连接超时
            .readTimeout(180000, TimeUnit.SECONDS)    // 读取超时
            .writeTimeout(180000, TimeUnit.SECONDS)   // 写入超时
            .build();

    private final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) {
        try {
            String response = new DeepSeekClient().chatCompletion("你好，介绍一下你自己");
            System.out.println("API Response:\n" + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String chatCompletion(String userMessage) throws IOException {
        // 构建请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("model", MODEL);

        List<Map<String, String>> messages = new ArrayList<>();
        Map<String, String> map1 = new HashMap<>();
        map1.put("role", "user");
        map1.put("content", userMessage);
        messages.add(map1);
        requestBody.put("messages", messages);

        requestBody.put("temperature", 0.7);
        requestBody.put("max_tokens", 1000);

        // 创建HTTP请求
        Request request = new Request.Builder()
                .url(API_URL)
                .post(RequestBody.create(
                        objectMapper.writeValueAsString(requestBody),
                        MediaType.parse("application/json")
                ))
                .addHeader("Authorization", "Bearer " + API_KEY)
                .addHeader("Content-Type", "application/json")
                .build();

        // 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code: " + response);
            }

            // 解析响应
            Map<String, Object> responseMap = objectMapper.readValue(
                    response.body().string(),
                    Map.class
            );

            // 提取生成的回复内容
            List<Map<String, Object>> choices = (List<Map<String, Object>>) responseMap.get("choices");
            if (choices != null && !choices.isEmpty()) {
                Map<String, Object> firstChoice = choices.get(0);
                Map<String, String> message = (Map<String, String>) firstChoice.get("message");
                return message.get("content");
            }
            return "No response content";
        }
    }
}
